package com.itheima.security;

import com.qiniu.util.Md5;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Component 配合 <context:component-scan base-package="com.itheima.security"></context:component-scan>
public class MyUserService implements UserDetailsService {
    private  BCryptPasswordEncoder passwordEncoder;

    public BCryptPasswordEncoder getPasswordEncoder() {
        return passwordEncoder;
    }

    public void setPasswordEncoder(BCryptPasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    //模拟数据库中的用户数据
    public   Map<String, com.itheima.pojo.User> map = new HashMap<>();
    public void initData(){
        com.itheima.pojo.User user1 = new com.itheima.pojo.User();
        user1.setUsername("admin");
        user1.setPassword(passwordEncoder.encode("admin"));

        com.itheima.pojo.User user2 = new com.itheima.pojo.User();
        user2.setUsername("xiaoming");
        user2.setPassword(passwordEncoder.encode("xiaoming"));

        map.put(user1.getUsername(),user1);
        map.put(user2.getUsername(),user2);
    }

    /**
     *
     * @param username 就是我们登录时输入的账号
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        initData();
        // 根据用户名查询用户的密码 ，id
        com.itheima.pojo.User user = map.get(username);
        if(null == user){
            return null;
        }
        // 根据用户id查询用户的权限集合

        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        authorities.add(new SimpleGrantedAuthority("ROLE_admin"));
        if(username.equals("admin")){
            authorities.add(new SimpleGrantedAuthority("checkitem_del"));
        }
        authorities.add(new SimpleGrantedAuthority("checkitem_add"));

        return new User(user.getUsername(),user.getPassword(),authorities);
    }

    public static void main(String[] args) {

         BCryptPasswordEncoder passwordEncoder;


        //salt
        String pwd =  "1234";
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        long bs = System.currentTimeMillis();
        String encode = encoder.encode(pwd);
        long be = System.currentTimeMillis();
        System.out.println("BCrypt:" + (be - bs));


        long md5s = System.currentTimeMillis();
        Md5.md5(pwd.getBytes());
        long md5e = System.currentTimeMillis();
        System.out.println("md5:" + (md5e - md5s));
    }
}
